package org.example.enums;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author liyishan
 * @date 2023/7/21 17:32
 * @apiNote
 */

public enum ResponseEnum {
    OK("R_200", "Success"),
    TOKEN_INVALID("R_20301", "Token invalid"),
    IP_INVALID("R_20302", "Ip invalid"),
    FAILURE("R_500", "Service exception"),
    NO_RESOURCE("R_404", "Resource does not exist"),
    OUT_RANGE("R_900", "Number out range");

    private final String code;
    private final String message;

    public static ResponseEnum ofCode(String code) {
        Optional<ResponseEnum> any = Arrays.stream(values()).filter((type) -> {
            return type.getCode().equals(code);
        }).findFirst();
        return (ResponseEnum)any.orElse((ResponseEnum) null);
    }

    public static ResponseEnum ofName(String name) {
        try {
            return valueOf(name);
        } catch (IllegalArgumentException var2) {
            return null;
        }
    }

    public String getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    private ResponseEnum(final String code, final String message) {
        this.code = code;
        this.message = message;
    }
}
